package Test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

public class Test1_1 extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField name;
	private JTextField uid;
	private JPasswordField pwd;
	private JPasswordField repwd;
	private JTextField phone;
	private JTextField mail;
	private JComboBox ageCbox;
	private Test1_1DAO tdao = new Test1_1DAO();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Test1_1 dialog = new Test1_1();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Test1_1() {
		setTitle(":: 회원 가입 ::");
		setBounds(100, 100, 460, 360);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel txtName = new JLabel("이름");
		txtName.setFont(new Font("굴림", Font.BOLD, 12));
		
		name = new JTextField();
		name.setColumns(10);
		
		JLabel txtUid = new JLabel("아이디");
		txtUid.setFont(new Font("굴림", Font.BOLD, 12));
		
		uid = new JTextField();
		uid.setColumns(10);
		
		pwd = new JPasswordField();
		
		repwd = new JPasswordField();
		
		JLabel txtPwd = new JLabel("비밀번호");
		txtPwd.setFont(new Font("굴림", Font.BOLD, 12));
		
		JLabel txtRepwd = new JLabel("비밀번호확인");
		txtRepwd.setFont(new Font("굴림", Font.BOLD, 12));
		
		JLabel txtGender = new JLabel("성별");
		txtGender.setFont(new Font("굴림", Font.BOLD, 12));
		
		ButtonGroup group = new ButtonGroup();
		final JRadioButton isMale = new JRadioButton("남자");
		isMale.setSelected(true);
		isMale.setFont(new Font("나눔고딕코딩", Font.PLAIN, 12));
		
		final JRadioButton isFemale = new JRadioButton("여자");
		isFemale.setFont(new Font("나눔고딕코딩", Font.PLAIN, 12));
		
		group.add(isMale);
		group.add(isFemale);
		
		JButton checkBtn = new JButton("중복확인");
//		checkBtn.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				if ( tdao.)
//					JOptionPane.showMessageDialog(null, "비밀번호가 일치하지 않슴돠!");
//				else{ 
//					Test1_1VO t = new Test1_1VO
//							(name.getText(), uid.getText(), new String (pwd.getPassword()),  
//									age,gender, phone.getText(), mail.getText());
//					if(tdao.insertMember(t) > 0 )
//						JOptionPane.showMessageDialog(null, "회원가입됨!");
//				
//			}
//		});
		checkBtn.setFont(new Font("나눔고딕코딩", Font.PLAIN, 12));
		
		JLabel txtAge = new JLabel("연령");
		txtAge.setFont(new Font("굴림", Font.BOLD, 12));
		
		ageCbox = new JComboBox();
		ageCbox.setModel(new DefaultComboBoxModel(new String[] {"10대", "20대", "30대", "40대", "50대이상"}));
		
		JLabel txtPhone = new JLabel("휴대폰번호");
		txtPhone.setFont(new Font("굴림", Font.BOLD, 12));
		
		JLabel txtMail = new JLabel("이메일");
		txtMail.setFont(new Font("굴림", Font.BOLD, 12));
		
		phone = new JTextField();
		phone.setColumns(10);
		
		mail = new JTextField();
		mail.setColumns(10);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtName)
						.addComponent(txtPwd)
						.addComponent(txtGender)
						.addComponent(txtRepwd)
						.addComponent(txtUid)
						.addComponent(txtAge)
						.addComponent(txtPhone)
						.addComponent(txtMail))
					.addGap(53)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(ageCbox, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
								.addComponent(mail, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
								.addComponent(phone, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
								.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
									.addComponent(isMale, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 116, Short.MAX_VALUE)
									.addComponent(isFemale, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE))
								.addGroup(Alignment.LEADING, gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(repwd, Alignment.LEADING)
									.addComponent(pwd, Alignment.LEADING)
									.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(uid))
									.addComponent(name, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)))
							.addGap(2)
							.addComponent(checkBtn, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
							.addGap(169))))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtName)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(10)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtUid)
						.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(checkBtn))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtPwd))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtRepwd)
						.addComponent(repwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtGender)
						.addComponent(isFemale)
						.addComponent(isMale))
					.addGap(8)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtAge)
						.addComponent(ageCbox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtPhone)
						.addComponent(phone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtMail)
						.addComponent(mail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(34, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton joinBtn = new JButton("가입완료");
				joinBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// 비밀번호와 비밀번호확인을 비교
						String gender = null;
						String age = null;
						if (isMale.isSelected())
							gender = "남";
						else if (isFemale.isSelected())
							gender = "여";
						
						age = (String) ageCbox.getSelectedItem();
						
						
						if ( ! Arrays.equals(pwd.getPassword(), repwd.getPassword()))
							JOptionPane.showMessageDialog(null, "비밀번호가 일치하지 않슴돠!");
						else{ 
							Test1_1VO t = new Test1_1VO
									(name.getText(), uid.getText(), new String (pwd.getPassword()),  
											age,gender, phone.getText(), mail.getText());
							if(tdao.insertMember(t) > 0 )
								JOptionPane.showMessageDialog(null, "회원가입됨!");
							dispose();
							
							
						}
					}
				});
				joinBtn.setFont(new Font("굴림", Font.BOLD, 12));
				joinBtn.setActionCommand("OK");
				buttonPane.add(joinBtn);
				getRootPane().setDefaultButton(joinBtn);
			}
		}
	}
}
